package com.xianyun.site.support.executor.strategy.commond;

import com.alibaba.cola.dto.Response;
import com.xianyun.site.support.common.define.FunctionModelEnum;
import com.xianyun.site.support.common.define.SupportTypeEnum;
import com.xianyun.site.support.common.define.SystemTypeEnum;
import com.xianyun.site.support.common.define.UserTypeEnum;
import com.xianyun.site.support.common.exception.SupportErrorCode;
import com.xianyun.site.support.common.exception.SupportException;
import com.xianyun.site.support.dto.ReceiveCmd;
import com.xianyun.site.support.dto.clientobject.CheckParamCO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.validation.Valid;

/**
 * @author mac_zyj
 */
@Slf4j
@Component
public class OperateCodeAnalysisExo {
    private static final  int MIN_OPERATE_CODE_LENGTH = 5;

    public Response execute(@Valid ReceiveCmd receiveCmd){
        String operateCode = receiveCmd.getOperateCode();
        String[] codeArrays = operateCode.split("-");
        validOperateCode(codeArrays);
        analysisOperateCode(codeArrays,receiveCmd);
        return Response.buildSuccess();
    }

    private void validOperateCode(String[] codeArrays){
        int length = codeArrays.length;
        if (length < MIN_OPERATE_CODE_LENGTH){
            throw  new SupportException(SupportErrorCode.SYS_SUPPORT_CODE_ERROR,"操作码位数有误");
        }
        checkOperateCodeFormat(codeArrays);
    }

    private void checkOperateCodeFormat(String[] codeArrays){
        for (String code : codeArrays){
            try {
                Assert.notNull(code,"操作码格式有误");
                Integer.parseInt(code);
            } catch (NumberFormatException numberFormatException){
                throw  new SupportException(SupportErrorCode.SYS_SUPPORT_CODE_ERROR,"操作码格式有误");
            }
        }
    }

    private void analysisOperateCode(String[] codeArrays,ReceiveCmd receiveCmd){
        CheckParamCO checkParam= receiveCmd.getCheckParam();
        checkParam.setUserType(userTypeAnalysis(Integer.parseInt(codeArrays[0])));
        checkParam.setUserAction(codeArrays[1]);
        checkParam.setSystemModel(systemModelAnalysis(Integer.parseInt(codeArrays[2])));
        checkParam.setModelFunction(functionModelAnalysis(Integer.parseInt(codeArrays[3])));
        checkParam.setOperationType(operationTypeAnalysis(Integer.parseInt(codeArrays[4])));
    }

    private String  userTypeAnalysis(Integer typeCode){
        String result = UserTypeEnum.getTypeNameByTypeId(typeCode);
        Assert.notNull(result,"操作码用户类别不存在");
        return  result;
    }

    private String operationTypeAnalysis(Integer operationType){
        String result =  SupportTypeEnum.getTypeNameByTypeId(operationType);
        Assert.notNull(result,"操作码操作类别不存在");
        return result;
    }

    private String systemModelAnalysis(Integer systemModelType){
        String result = SystemTypeEnum.getTypeNameByTypeId(systemModelType);
        Assert.notNull(result,"操作码系统模块不存在");
        return result;
    }

    private String functionModelAnalysis(Integer functionType){
        String result = FunctionModelEnum.getModelNameByTypeId(functionType.toString());
        Assert.notNull(result,"操作码功能模块不存在");
        return result;
    }

}
